﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace KiDS_Projekat
{
	/// <summary>
	/// Helper klasa za konektovanje na terminal, 
	/// u slucaju da terminal ne radi, ispis ce raditi na konzoli
	/// Lokacija i port na kome terminal slusa su podesivi u konfiguracionom fajlu
	/// </summary>
	public class TerminalConnector
	{
		Client _clientSocket = new Client(Properties.Settings.Default.TerminalLocation, Properties.Settings.Default.TerminalPort);
		bool _opened = false;
		Node _myNode;
		public TerminalConnector(Node myNode)
		{
			Console.WriteLine("Uspostavljanje konekcije ka terminalu...");
			_myNode = myNode;
			_opened = _clientSocket.Open();
			new Task(() => Run(myNode)).Start();
		}


		public void Run(Node myNode)
		{
			_opened =_clientSocket.SendASCIIMessage(myNode.Location, 2);
			_opened = _clientSocket.SendASCIIMessage(myNode.Port + "", 3);

		}

		public void Print(string message, byte type = 1)
		{
			if (_opened)
			{
				new Task(() =>
				{
					_opened = _clientSocket.SendASCIIMessage(message, type);
				}).Start();
			}
			else Console.WriteLine(message);
			
		}

		public void CloseConnection()
		{
			_clientSocket.Close();
		}
	}
}
